package com.one2manycall.controller;

import com.one2manycall.service.websocket.UserRegistry;
import com.one2manycall.vo.UserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * 发布视频
 */
@Controller
@RequestMapping("/DeployController")
public class DeployController {


    @Autowired
    private UserRegistry userRegistry;


    //// 页面部分 开始  /////////////////////////////////////
    @GetMapping("/home")
    public String uploadFilePage(Model model) {
        return "html/file/upload.html";
    }

    @Autowired
    FileUploadController fileUploadController;

    @PostMapping("/upload")
    public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file,
                                                   @RequestParam("filename") String customFileName,
                                                   @RequestParam("userName") String userName,
                                                   @RequestParam("token") String token
    ) {


        return fileUploadController.handleFileUpload(file,customFileName,userName,token);

    }



    // 这里假设视频文件在项目的"static"目录下，名为video.webm，实际中可按需替换真实路径
    private static final String VIDEO_PATH = "/video.webm";

    @GetMapping("/videoPath")
    public String getVideoPath() {
        return VIDEO_PATH;
    }


    @GetMapping("/videoPath/next")
    public String getVideoPathNext() {
        return VIDEO_PATH;
    }




    @GetMapping("/views")
    @ResponseBody
    public Object getViews() {

        ConcurrentMap<String, UserSession> all = userRegistry.getAll();

        HashMap<String, String> stringStringHashMap = new HashMap<>();

        all.entrySet().stream().forEach(a->stringStringHashMap.put(a.getKey(),a.getValue().getSessionId()));

        return stringStringHashMap;
    }


}